<template>
  <view class="content">
    <view class="log-layout">
      <view>选择接口地址：</view>
      <radio-group @change="radioChange">
        <view class="hor-layout-center" v-for="(item, index) in urlItems" :key="item.value">
          <radio :value="item.value" :checked="index === current"/>
          <view>{{ item.name }}</view>
        </view>
      </radio-group>
    </view>
    <button type="primary" @click="setsign">setsign</button>
    <button type="primary" style="margin-top: 10px" @click="readLogList">日志文件列表</button>
    <view class="log-layout">
      <view>请求日志：</view>
      <view class="log-item" v-for="(item,index) in logList" :key="index">
        {{ item }}
      </view>
    </view>
  </view>
</template>

<script>
import msbsign from '@/common/msbsign.js';

export default {
  data() {
    return {
      title: 'Hello',
      current: 1,
      urlItems: [
        {
          name: 'app.zwfw.nmg.gov.cn',
          value: 'https://app.zwfw.nmg.gov.cn/aep/hlwyy/business-cxfw/zcfg/zcfgList',
          checked: 'true'
        },
        {
          name: 'mdej.impc.com.cn',
          value: 'https://mdej.impc.com.cn/hlwyy/business-cxfw/zcfg/zcfgList',
        },
        {
          name: 'admin.xiqueque.com',
          value: 'http://admin.xiqueque.com/api/admin/agent/all_agencies_list?saas_app_id=2099&lang=cn',
        }
      ],
      fileList: [],
      logList: []
    }
  },
  onLoad() {
    // 添加请求拦截器
    uni.addInterceptor('request', {
      invoke(args) {
        // 在发送请求前做一些处理
        // args是请求参数，可以修改args中的参数
        // 例如：args.header['Authorization'] = 'Bearer ' + getToken();
        console.log("args", args)
      },
      success(args) {
        // 在请求成功后做一些处理
        // args是请求返回的参数
      },
      fail(err) {
        // 在请求失败后做一些处理
        // err是请求失败的错误信息
      },
      complete() {
        // 请求结束的处理
      }
    });
  },
  methods: {
    radioChange(mItem) {
      console.log("radioChange", mItem)
      this.urlItems.forEach((item, index) => {
        if (mItem.detail.value == item.value) this.current = index
      })
      console.log("radioChange", this.current)
    },
    setsign() {
      this.logList = []
      var muuid = msbsign.generateUUID();//自己生成UUD
      var mtimestamp = new Date().getTime(); //时间戳
      var maep_key = "e8558a59008f4ce58e6d076b2631b2cf"//你的aep_key
      var signparam = {
        appKeyvalue: maep_key,
        appSecretValue: "7ee49097a212438d9be49bbafe65d8c1",//你的aep_secret
        uuid: muuid,
        timestamp: mtimestamp
      };
      let url = this.urlItems[this.current].value
      console.log("uni.request", url)
      this.logList.push(`"uni.request", ${url}`)
      uni.request({
        url: url, //代理地址
        data: {},
        header: {
          'x-aep-nonce': muuid,
          'x-aep-appkey': maep_key,
          'x-aep-timestamp': mtimestamp,
          'x-aep-encrypt-sm3': 'true',
          'x-aep-signature': msbsign.sm3ApiSign(signparam),
          'Connection': 'keep-alive', // 开启长连接
          'content-type': 'application/json', // 默认值
        },
        success: (res) => {
          this.logList.push(`"request=====success", ${JSON.stringify(res)}`)
          console.log("request=====", res)
          let {data: {data: {data: result}}} = res
          console.log("request====", result)
        },
        fail: (err) => {
          this.logList.push(`"request=====fail", ${JSON.stringify(err)}`)
          console.error("request====", err)
        }
      });
    },
    async readLogList() {
      let fileList = await this.$LogCat.readFiles()
      fileList = fileList && fileList.map(item => item.name) || []
      this.fileList = fileList
      uni.showActionSheet({
        title: "日志文件列表",
        itemList: fileList,
        success: async (e) => {
          let fileName = this.fileList[e.tapIndex]
          console.log("readLogList===", fileName)
          let content = await this.$LogCat.readLogFile(fileName)
          console.log("readLogList===", content)
          this.showLogDialog(fileName, content)
        }
      })
    },
    showLogDialog(title, content) {
      uni.showModal({
        title: title,
        content: content,
        success: (res) => {
          this.exeRet = JSON.stringify(res)
        },
        fail: (res) => {
          this.exeRet = JSON.stringify(res)
        }
      })
    }
  }
}
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.hor-layout-center {
  display: flex;
  flex-direction: row;
}

.log-layout {
  width: calc(100% - 20 * 4px);
  margin: 20px;
  padding: 20px;
  background: #f1ecec;
  border-radius: 10px;
}

.log-item {
  margin-bottom: 10px;
  word-break: break-word;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
}
</style>
